package com.gzw.scm.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class DateUtil {
    @SuppressLint("SimpleDateFormat")
    public static String ToString(Date _date) {
        return ToString(_date, "");
    }

    public static String ToString(Date _date, String _f) {
        String stringDate = "";
        String f = _f;
        Date date = null;
        if (f.length() == 0) {
            f = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(f, Locale.ENGLISH);
            stringDate = formatter.format(_date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringDate;
    }

    public static String ToString(String _dateString) {
        return ToString(_dateString, "");
    }

    public static String ToString(String _dateString, String _f) {
        String stringDate = "";
        String f = _f;
        if (f.length() == 0) {
            f = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(f, Locale.ENGLISH);
            stringDate = formatter.parse(_dateString).toString();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return stringDate;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date ToDate(String _date) {
        String s3 = _date;
        if (_date.indexOf("T") > 0) {
            String s1 = _date.substring(0, 10);
            String s2 = _date.substring(11, 19);
            s3 = s1 + " " + s2;
        }
        Date date = null;
        try {
            DateFormat dateFormat;
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = dateFormat.parse(s3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String[] getYMD(String _date) {
        String[] YMD = new String[3];
        Calendar cal = Calendar.getInstance();// 使用日历类
        int toYear = cal.get(Calendar.YEAR);// 得到年
        int toMonth = cal.get(Calendar.MONTH) + 1;// 得到月，因为从0开始的，所以要加1
        int toDay = cal.get(Calendar.DAY_OF_MONTH);// 得到天
        Date date = new Date();
        try {
            Calendar cal2 = Calendar.getInstance();// 使用日历类
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
            date = formatter.parse(_date);
            cal2.setTime(date);
            int year = cal2.get(Calendar.YEAR);// 得到年
            int month = cal2.get(Calendar.MONTH) + 1;// 得到月，因为从0开始的，所以要加1
            int day = cal2.get(Calendar.DAY_OF_MONTH);// 得到天
            if (toYear == year & toMonth == month & toDay == day) {
                YMD[0] = "今天";
                YMD[1] = "今天";
                YMD[2] = "今天";
            } else if (toYear == year & toMonth == month & toDay - 1 == day) {
                YMD[0] = "昨天";
                YMD[1] = "昨天";
                YMD[2] = "昨天";
            } else {
                YMD[0] = String.valueOf(year);
                YMD[1] = String.valueOf(month);
                YMD[2] = String.valueOf(day);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return YMD;
    }

    public static String getFileName() {
        String newFileName = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyyMMddHHmm_ss", Locale.ENGLISH);
            newFileName = formatter.format(new Date()).toString();

            String str1 = String.valueOf(new Random().nextInt(9));
            String str2 = String.valueOf(new Random().nextInt(9));
            String str3 = String.valueOf(new Random().nextInt(9));
            String str4 = String.valueOf(new Random().nextInt(9));
            return "IMG" + newFileName + str1 + str2 + str3 + str4 + ".jpg";
        } catch (Exception localException) {
            return null;
        }
    }
}
